package com.lanou.sfu.songforu.music_library_fragment;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.adapter.MusicLibraryArtistAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.constant.Url;
import com.lanou.sfu.songforu.entity.ArtistListEntity;
import com.lanou.sfu.songforu.singleton.VolleySingleton;
import com.lanou.sfu.songforu.view.ReplaceFragment;

import java.util.List;

/**
 * Created by dllo on 15/10/21.
 */
public class ArtistFragment extends BaseFragment implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private View v;
    private ViewPager mViewPager;
    private LinearLayout viewPagerContainer;
    private Context context;
    private VolleySingleton volleySingleton;
    private StringRequest request;
    private List<ArtistListEntity.ArtistEntity> artistList;
    private Button chineseMan, chineseWoman, chineseComp, westermMan, westermWoman, westermComp, koreaMan, koreaWoman, koreaComp, japaneseMan, japaneseWoman, japaneseComp, artist_otherClass, artist_more;
    private MusicLibraryArtistAdapter adapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.viewpager_artist, null);
        context = getActivity();
        mViewPager = (ViewPager) v.findViewById(R.id.fragment_artist_viewPager);
        viewPagerContainer = (LinearLayout) v.findViewById(R.id.viewpagerContainer);
        chineseMan = (Button) v.findViewById(R.id.chineseMan);
        chineseWoman = (Button) v.findViewById(R.id.chineseWoman);
        chineseComp = (Button) v.findViewById(R.id.chineseComp);
        westermMan = (Button) v.findViewById(R.id.westermMan);
        westermWoman = (Button) v.findViewById(R.id.westermWoman);
        westermComp = (Button) v.findViewById(R.id.westermComp);
        koreaMan = (Button) v.findViewById(R.id.koreaMan);
        koreaWoman = (Button) v.findViewById(R.id.koreaWoman);
        koreaComp = (Button) v.findViewById(R.id.koreaComp);
        japaneseMan = (Button) v.findViewById(R.id.japaneseMan);
        japaneseWoman = (Button) v.findViewById(R.id.japaneseWoman);
        japaneseComp = (Button) v.findViewById(R.id.japaneseComp);
        artist_otherClass = (Button) v.findViewById(R.id.artist_otherClass);
        artist_more = (Button) v.findViewById(R.id.artist_more);

        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String url = Url.ARTIST_FRAGMENT;
        volleySingleton = VolleySingleton.getInstance(context);
        request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {


                Gson gson = new Gson();
                ArtistListEntity artistListEntity = gson.fromJson(response, ArtistListEntity.class);
                artistList = artistListEntity.getArtist();
                mViewPager.setOffscreenPageLimit(3);
                if (artistList != null) {
                    adapter = new MusicLibraryArtistAdapter(context, artistList);
                }
                    mViewPager.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);



        chineseMan.setText(R.string.CHINESEMAN);
        chineseMan.setOnClickListener(this);
        chineseWoman.setText(R.string.CHINESEWOMAN);
        chineseWoman.setOnClickListener(this);
        chineseComp.setText(R.string.CHINESECOMP);
        chineseComp.setOnClickListener(this);
        westermMan.setText(R.string.WESTERMMAN);
        westermMan.setOnClickListener(this);
        westermWoman.setText(R.string.WESTERMWOMAN);
        westermWoman.setOnClickListener(this);
        westermComp.setText(R.string.WESTERMCOMP);
        westermComp.setOnClickListener(this);
        koreaMan.setText(R.string.KOREAMAN);
        koreaMan.setOnClickListener(this);
        koreaWoman.setText(R.string.KOREAWOMAN);
        koreaWoman.setOnClickListener(this);
        koreaComp.setText(R.string.KOREACOMP);
        koreaComp.setOnClickListener(this);
        japaneseMan.setText(R.string.JAPANESEMAN);
        japaneseMan.setOnClickListener(this);
        japaneseWoman.setText(R.string.JAPANESEWOMAN);
        japaneseWoman.setOnClickListener(this);
        japaneseComp.setText(R.string.JAPANESECOMP);
        japaneseComp.setOnClickListener(this);
        artist_otherClass.setText(R.string.ARTIST_OTHER);
        artist_otherClass.setOnClickListener(this);
        mViewPager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.pager_margin));
        mViewPager.setOnPageChangeListener(this);
        artist_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new ReplaceFragment(new HotArtistAllFragment(), getActivity().getSupportFragmentManager(), R.id.fl_main, true, null);
            }
        });

        viewPagerContainer.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // dispatch the events to the ViewPager, to solve the problem that we can swipe only the middle view.
                return mViewPager.dispatchTouchEvent(event);
            }
        });

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (viewPagerContainer != null) {
            viewPagerContainer.invalidate();
        }
    }

    @Override
    public void onClick(View v) {
        Bundle bundle = new Bundle();
        switch (v.getId()) {
            case R.id.chineseMan:
                bundle.putInt(getString(R.string.area), 6);
                bundle.putInt(getString(R.string.sex), 1);
                bundle.putString(getString(R.string.titleText),getString(R.string.CHINESEMAN));
                break;
            case R.id.chineseWoman:
                bundle.putInt(getString(R.string.area), 6);
                bundle.putInt(getString(R.string.sex), 2);
                bundle.putString(getString(R.string.titleText), getString(R.string.CHINESEWOMAN));
                break;
            case R.id.chineseComp:
                bundle.putInt(getString(R.string.area), 6);
                bundle.putInt(getString(R.string.sex), 3);
                bundle.putString(getString(R.string.titleText),getString(R.string.CHINESECOMP));
                break;
            case R.id.westermMan:
                bundle.putInt(getString(R.string.area), 3);
                bundle.putInt(getString(R.string.sex), 1);
                bundle.putString(getString(R.string.titleText),getString(R.string.WESTERMMAN));
                break;
            case R.id.westermWoman:
                bundle.putInt(getString(R.string.area), 3);
                bundle.putInt(getString(R.string.sex), 2);
                bundle.putString(getString(R.string.titleText),getString(R.string.WESTERMWOMAN));
                break;
            case R.id.westermComp:
                bundle.putInt(getString(R.string.area), 3);
                bundle.putInt(getString(R.string.sex), 3);
                bundle.putString(getString(R.string.titleText),getString(R.string.WESTERMCOMP));
                break;
            case R.id.koreaMan:
                bundle.putInt(getString(R.string.area), 7);
                bundle.putInt(getString(R.string.sex), 1);
                bundle.putString(getString(R.string.titleText),getString(R.string.KOREAMAN));
            case R.id.koreaWoman:
                bundle.putInt(getString(R.string.area), 7);
                bundle.putInt(getString(R.string.sex), 2);
                bundle.putString(getString(R.string.titleText),getString(R.string.KOREAWOMAN));
                break;
            case R.id.koreaComp:
                bundle.putInt(getString(R.string.area), 7);
                bundle.putInt(getString(R.string.sex), 3);
                bundle.putString(getString(R.string.titleText),getString(R.string.KOREACOMP));
                break;
            case R.id.japaneseMan:
                bundle.putInt(getString(R.string.area), 60);
                bundle.putInt(getString(R.string.sex), 1);
                bundle.putString(getString(R.string.titleText),getString(R.string.JAPANESEMAN));
                break;
            case R.id.japaneseWoman:
                bundle.putInt(getString(R.string.area), 60);
                bundle.putInt(getString(R.string.sex), 2);
                bundle.putString(getString(R.string.titleText),getString(R.string.JAPANESEWOMAN));
                break;
            case R.id.japaneseComp:
                bundle.putInt(getString(R.string.area), 60);
                bundle.putInt(getString(R.string.sex), 3);
                bundle.putString(getString(R.string.titleText),getString(R.string.JAPANESECOMP));
                break;
            case R.id.artist_otherClass:
                bundle.putInt(getString(R.string.area), 5);
                bundle.putInt(getString(R.string.sex), 0);
                bundle.putString(getString(R.string.titleText),getString(R.string.ARTIST_OTHER));
                break;
        }
        ArtistClassDetailFragment fragment = new ArtistClassDetailFragment();
        fragment.setArguments(bundle);
        new ReplaceFragment(fragment, getActivity().getSupportFragmentManager(), R.id.fl_main, true, null);
    }
}
